<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="shop_id">
           <el-select v-model="filters.shop_id" placeholder="所属门店">
                <el-option
                v-for="item in shopIds"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="pro_id">
           <el-select v-model="filters.pro_id" placeholder="单品名称">
                <el-option
                v-for="item in products"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="startTime">
            <el-date-picker
                v-model="filters.startTime"
                type="date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="开始时间" class="input_postion">
            </el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime">
            <el-date-picker
                v-model="filters.endTime"
                type="date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="结束时间" class="input_postion">
            </el-date-picker>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      :showEditOperation="false"
      :showBatchDelete="false"
      :showOperation="false"
      :showDeleteOperation="false"
      @findPage="findPage"
      @selectionChange="selectionChange"
      ref="CyTable"
    ></cy-table>
  </div>
</template>

<script>
import CyTable from "@/views/Core/CyTable";
import KtButton from "@/views/Core/KtButton";
import { format } from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";
export default {
  components: {
    CyTable,
    KtButton,
    TableColumnFilterDialog
  },
  data() {
    return {
      size: "small",
      filters: {
        shop_id: "",
        pro_id: "",
        startTime: "",
        endTime: "",
        t: "productStatistics",
        queryname: ""
      },
      shopIds: [{'value':'','label':'请选择'}],
      products: [{'value':'','label':'请选择'}],
      statusOption: [{
        status: '',
        label: '请选择'
      }, {
        status: 'N',
        label: '可用'
      }, {
        status: 'Y',
        label: '不可用'
      }],
      columns: [
        { prop: "shopname", label: "所属门店", minWidth: 100},
        { prop: "name", label: "商品名称", minWidth: 100 },
        { prop: "stock", label: "库存", minWidth: 80 },
        { prop: "sales_num", label: "销售量", minWidth: 120},
        { prop: "totalNum", label: "总销量", minWidth: 120 },
        { prop: "totalAmount", label: "总销售额", minWidth: 100 }
	  ],
	  filterColumns:[],
      pageRequest: { pageNum: 1, pageSize: 10 },
      pageResult: {},

      operation: false, // true:新增, false:编辑
      editDialogVisible: false, // 新增编辑界面是否显示
      editLoading: false,
      dataFormRules: {
        sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
        CarouselFigurePic: [{ required: true, message: "请上传轮播图", trigger: "blur" }],
        config_id: [{ required: true, message: "请选择类型", trigger: "blur" }],
        status: [{ required: true, message: "请输入选择是否可用", trigger: "blur" }]
      },
    };
  },
  methods: {
    // 获取分页数据
    findPage: function(data) {
      this.$refs.CyTable.findPage(this.filters);
    },
    selectionChange(data){
      console.log(data);
      this.idArray = data.selections;
    },
    handleAvatarSuccess_arousel_figure_img(res, file) {
        this.dataForm.CarouselFigurePic = res.data.imgUrl
    },
    beforeAvatarUpload(file) {
      var isJPG =false;
      if(file.type === 'image/jpeg' || file.type === 'image/png'){
        isJPG = true;
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message({
              message: '上传头像图片只能是 JPG 或 PNG 格式!',
              type: 'error',
              center:true,
              offset:250,
              duration:1000
        });
      }
      if (!isLt2M) {
         this.$message({
              message: '上传头像图片大小不能超过 2MB!',
              type: 'error',
              center:true,
              offset:250,
              duration:1000
        });
      }
      return isJPG && isLt2M;
    },
    // 显示新增界面
    handleAdd: function() {
      this.editDialogVisible = true;
      this.operation = true;
      this.resetForm("dataForm");
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.editDialogVisible = true;
      this.operation = false;
      this.$nextTick(function() {
        this.dataForm = Object.assign({}, params.row);
      });
    },
    // 时间格式化
    dateFormat: function(row, column, cellValue, index) {
      return format(row[column.property]);
    },

    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].resetFields();
        this.findPage(null);
      }
    },

    // 处理表格列过滤显示
    displayFilterColumnsDialog: function() {
      this.$refs.tableColumnFilterDialog.setDialogVisible(true);
    },
    // 处理表格列过滤显示
    handleFilterColumns: function(data) {
      this.filterColumns = data.filterColumns;
      this.$refs.tableColumnFilterDialog.setDialogVisible(false);
    },
    // 处理表格列过滤显示
    initColumns: function() {
      var temp = [];

      $.each(this.columns, function(key, val) {
        temp.push(val);
      });
      this.filterColumns = temp;
    },
    //列表下载
    downloadExcel() {
      this.$confirm("确定下载列表文件?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let filename = "轮播图管理下载";
          this.$refs.CyTable.exportData(
            this.filters,
            this.filterColumns,
            filename
          );
        })
        .catch(() => {});
    }
  },
  mounted() {
    this.initColumns();
    var this_=this;
    this.utils.request.queryUserList({'t': 'shop'}, function(data) {
        if(null!=data.data){
            for(var i=0;i<data.data.length;i++){
                var obj={};
                obj.value=data.data[i].id;
                obj.label=data.data[i].name;
                this_.shopIds.push(obj);
            }
        }
    });
    
    this.utils.request.queryUserList({'t': 'product'}, function(data) {
        if(null!=data.data){
            for(var i=0;i<data.data.length;i++){
                var obj={};
                obj.value=data.data[i].id;
                obj.label=data.data[i].name;
                this_.products.push(obj);
            }
        }
    });
  }
};
</script>

<style scoped>
</style>